﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ServiceModel.Syndication;
using MShop.Core.Util;
using MShop.Core.Domain;
using MShop.Core.DataAccess;
using MShop.Core;

namespace MShop.Web.Controllers
{
    [HandleError]
    public class HomeController : ControllerBase
    {
        private readonly ICommonDao<Announce> _CommonDao;
        private readonly IProductDao _ProductDao;
        private readonly IArticleDao _ArticleDao;
        private readonly ICommonDao<HelpCategory> _CommonDao2;
        private readonly ICartItemDao _CartItemDao;
        private readonly ICommonDao<Ad> _AdDao;
        private readonly IItemCatDao _ItemCatDao;
        private readonly IShopContext _ShopContext;
        public HomeController(ICommonDao<Announce> commonDao,IProductDao productDao,IArticleDao articleDao,
            ICommonDao<HelpCategory> commonDao2, IShopContext shopContext, ICartItemDao cartItemDao,ICommonDao<Ad> adDao,IItemCatDao itemCatDao) 
        {
            _CommonDao = commonDao;
            _ProductDao = productDao;
            _ArticleDao = articleDao;
            _CommonDao2 = commonDao2;
            _ShopContext = shopContext;
            _CartItemDao = cartItemDao;
            _ItemCatDao = itemCatDao;
            _AdDao = adDao;
        }


        public ActionResult Index()
        {
            AppendMeta("", _ShopContext.Settings.WebSiteKeyWords,_ShopContext.Settings.WebSiteDescription);

            ViewData["hotproducts"] = _ProductDao.Find(x => x.IsRecommend && x.IsPublish && x.IsDelete == false && x.Status==1).OrderBy(x => x.Sort).Take(3);

            ViewData["newproducts"] = _ProductDao.Find(x => x.IsPublish && x.IsDelete == false && x.Status == 1).OrderBy(x => x.Sort).Take(3);

          

            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        [OutputCache(Duration = 600, VaryByParam = "*", Location = System.Web.UI.OutputCacheLocation.Any)]
        public ActionResult GetProduct(int region)
        {
            //ViewData["products1"] = _ProductDao.Find(x => x.Region == 2);//意大利
            //ViewData["products2"] = _ProductDao.Find(x => x.Region == 1);//法国
            //ViewData["products2"] = _ProductDao.Find(x => x.Region == 3);//西班牙
           
            var q = _ProductDao.Find(x => x.Region== region&&x.IsPublish && x.IsDelete == false && x.Status == 1).OrderBy(x => x.Sort).Take(4);
            return PartialView("product/getproduct",q);
        }


        [OutputCache(Duration = 600, VaryByParam = "cid", Location = System.Web.UI.OutputCacheLocation.Any)]
        public ActionResult GetProductByCid(int cid)
        {
            //ViewData["products1"] = _ProductDao.Find(x => x.Region == 2);//意大利
            //ViewData["products2"] = _ProductDao.Find(x => x.Region == 1);//法国
            //ViewData["products2"] = _ProductDao.Find(x => x.Region == 3);//西班牙

            var q = _ProductDao.Find(x => x.CId == cid && x.IsPublish && x.IsDelete == false && x.Status == 1).OrderBy(x => x.Sort).Take(4);
            return PartialView("product/getproduct", q);
        }

        [OutputCache(Duration = 600, VaryByParam = "*", Location = System.Web.UI.OutputCacheLocation.Any)]
        public ActionResult Header()
        {
            decimal totalfee = 0m;
            IQueryable<CartItem> cartitems= _CartItemDao.GetCurrentCartItems(User.Identity.IsAuthenticated, _ShopContext, out totalfee);
            ViewData["cartitemcount"] = cartitems.Count();
            ViewData["totalfee"] = totalfee;
            return PartialView("header");
        }
        [OutputCache(Duration = 600, VaryByParam = "*", Location = System.Web.UI.OutputCacheLocation.Any)]
        public ActionResult Footer()
        {
            ViewData["articles"] = _ArticleDao.Find(x => x.CId == 11);
            ViewData["helps"] = _CommonDao2.Find(x=>x.IsMain).OrderBy(x=>x.Sort);
            return PartialView("Footer");
        }

        [OutputCache(Duration = 600, VaryByParam = "*", Location = System.Web.UI.OutputCacheLocation.Any)]
        public ActionResult Announce()
        {
            var Model= _CommonDao.Find(x=>x.Status>0).OrderByDescending(x=>x.Sort).Take(4);
            return PartialView("AnnounceControl", Model);
        }

        [OutputCache(Duration=600,VaryByParam="*",Location= System.Web.UI.OutputCacheLocation.Any)]
        public ActionResult Article(int sort)
        {
            var Model = _ArticleDao.Find(x => x.ArticleCategory.Sort==sort).OrderByDescending(x => x.Click).Take(15);
            return PartialView("ArticleControl", Model);
        }



        [OutputCache(Duration = 600, VaryByParam = "*", Location = System.Web.UI.OutputCacheLocation.Any)]
        public ActionResult SlidePlay()
        {
            var Model = _AdDao.Find(x => x.Status == 1).OrderBy(x => x.Sort);
            return PartialView("slideplay", Model);
        }


        [OutputCache(Duration = 600, VaryByParam = "*", Location = System.Web.UI.OutputCacheLocation.Any)]
        public ActionResult Rss()
        {

            var Model = _ProductDao.GetValidProducts().OrderBy(x => x.Sort).Take(200);
            List<SyndicationItem> list = new List<SyndicationItem>();
            foreach (var item in Model)
            {
                
                string desc=string.Format("<img src='{0}' alt='{1}' />",item.GetThumbImageUrl(_ShopContext.Settings.WebSiteUrl),item.ProductName);
                desc +=item.Desc ;
                var m=  new SyndicationItem(item.ProductName,desc , new Uri(_ShopContext.Settings.WebSiteUrl + "/product/item/" + item.Id), item.Id.ToString(),
                        new DateTimeOffset(DateTime.Now));
                list.Add(m);
            }

            SyndicationFeed feed = new SyndicationFeed(string.Concat(_ShopContext.Settings.WebSiteTitle,"产品信息"), "显示前面200条产品记录", new Uri(_ShopContext.Settings.WebSiteUrl + "/rss"), list.AsEnumerable());

            var stream = Response.OutputStream;
            System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(stream);
            feed.SaveAsRss20(writer);
            writer.Close();
            return new EmptyResult();
        }


        [OutputCache(Duration = 600, VaryByParam = "*", Location = System.Web.UI.OutputCacheLocation.Any)]
        public ActionResult Catalog()
        {
            var Model = _ItemCatDao.GetAll();
            return PartialView(Model);
        }

        public ActionResult ProductList(int id)
        {
            ViewData["catname"] = _ItemCatDao.GetById(id).Name;
            var Model = _ProductDao.GetValidProducts().Where(x=>x.CId==id).OrderBy(x => x.Sort);
            return View(Model);
        }

    }
}
